﻿#region Imports
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
#endregion

namespace BulkRenamer.Util.MemoryMgmt
{
    /// <summary>
    /// 
    /// </summary>
    /// <seealso cref="http://msdn.microsoft.com/en-us/library/498928w2%28v=vs.100%29.aspx">
    /// Cleaning up unmanaged resources</seealso>
    /// <seealso cref="http://msdn.microsoft.com/en-us/library/ms229042%28v=vs.100%29.aspx">
    /// Design Guidelines for Developing Class Libraries</seealso>
    /// <seealso cref="http://msdn.microsoft.com/en-us/library/ms228969%28v=vs.100%29.aspx">
    /// Asynchronous Programming Design Patterns</seealso>
    public class Disposer : IDisposable
    {
        #region Memory management
        private IntPtr nativeResource = Marshal.AllocHGlobal(100);
        private Disposer managedResource = new Disposer();

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
            GC.Collect();
        }

        //~Destructor()
        //{
        //    Dispose(false);  // Finalizer calls Dispose(false)
        //}

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // free managed resources
                if (managedResource != null)
                {
                    managedResource.Dispose();
                    managedResource = null;
                }
            }

            // free native resources if there are any.
            if (nativeResource != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(nativeResource);
                nativeResource = IntPtr.Zero;
            }
        }
        #endregion
    }
}
